package com.online_vote_system.demo.controller;

import com.online_vote_system.demo.service.CompetitionService;
import com.online_vote_system.demo.service.PlayerService;
import com.online_vote_system.demo.vo.Competition;
import com.online_vote_system.demo.vo.Player;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Slf4j
@Controller
public class CompetitionController {

    @Autowired
    CompetitionService competitionService;
    @Autowired
    PlayerService playerService;

    @GetMapping("/CompetitionOs")
    public String Competition(Model model,@RequestParam(defaultValue = "") String flag){
        List<Competition> competitions = competitionService.getAll();
        System.out.println(competitions);
        model.addAttribute("competitions",competitions);
        if (flag.equals("root")){
            return "html/RootAdmin/competitionIndex";
        }else{
            return "html/Admin/competitionIndex";
        }
    }

    @GetMapping("/delCompetition/{flag}/{comId}")
    public String delCompetition(@PathVariable(value = "comId") String comId,
                                 @PathVariable(value = "flag") String flag,
                                 Model model){
        Boolean result = competitionService.removeById(comId);
        log.info(String.valueOf(result)+"::"+comId+":flag:"+flag);
        return Competition(model,flag);
    }

    @GetMapping("/changeType/{flag}/{comId}")
    public String changeType(@PathVariable(value = "comId") String comId,
                             @PathVariable(value = "flag") String flag,
                             @RequestParam(value = "type") String type,
                             Model model){
        competitionService.changeType(type,comId);
        return Competition(model,flag);
    }

    @GetMapping("/changeCompetition/{flag}/{comId}")
    public String changeCompetition(@PathVariable(value = "comId") String comId,
                                    @PathVariable(value = "flag") String flag,
                                    Model model){
        Competition competition = competitionService.getOne(comId);
        List<Player> players = playerService.list();
        model.addAttribute("competition",competition);
        model.addAttribute("players",players);
        if (flag.equals("root")){
            return "html/RootAdmin/competitionChange";
        }else{
            return "html/Admin/competitionChange";
        }
    }

    @PostMapping("/updateCompetition/{flag}")
    public String updateCompetition(String comId,
                                    String playerId_1,
                                    String playerId_2,
                                    String score1,
                                    String score2,
                                    String comName,
                                    Model model,
                                    @PathVariable(value = "flag") String flag){
        competitionService.updateOne(comId,playerId_1,playerId_2,score1,score2,comName);
        return Competition(model,flag);
    }

    @GetMapping("/toInsertCompetition/{flag}")
    public String toInsertCompetition(Model model,
                                 @PathVariable(value = "flag") String flag){
        List<Player> players = playerService.list();
        model.addAttribute("players",players);
        if (flag.equals("root")){
            return "html/RootAdmin/competitionInsert";
        }else{
            return "html/Admin/competitionInsert";
        }
    }
    @PostMapping("/insertCompetition/{flag}")
    public String insertCompetition(Model model,
                                    @PathVariable(value = "flag") String flag,
                                    String playerId_1,
                                    String playerId_2,
                                    String comName){
        competitionService.insertOne(playerId_1,playerId_2,comName);
        return Competition(model,flag);
    }



}
